package com.argticling.model;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import javax.persistence.*;
import java.util.List;

@Entity
public class Article implements Persistable {

    private DublinCore dc;
    private boolean published;
    private boolean ready;//not published, but waiting to be published
    private List<TextArticleFragment> textArticleFragments;
  //  private List<ImageArticleFragment> imageArticleFragments;
    private String authorId;
    private String id;

    public Article(){
    	dc = new DublinCore();
    }

    public Article(String titulo){
    	this(titulo, false);
    }

    public Article(String titulo, boolean published) {
        dc = new DublinCore();
        dc.setTitle(titulo);
        this.dc = dc;
        this.published = published;
    }


    @Id
    @Column(name = "id", updatable = false, nullable = false)
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }


    @OneToOne(cascade = CascadeType.ALL)
    public DublinCore getDc() {
        return dc;
    }

    public void setDc(DublinCore dc) {
        this.dc = dc;

    }

    @Column(name = "PUBLISHED")
    public boolean isPublished() {
        return published;
    }

    public void setPublished(boolean published) {
        this.published = published;
    }

    @Column(name = "Author_ID")
    public String getAuthorId() {
        return authorId;
    }

    public void setAuthorId(String authorId) {
        this.authorId = authorId;
    }

    public void addTextArticleFragment(TextArticleFragment articleFragment) {
        textArticleFragments.add(articleFragment);
    }

//    public void addImageArticleFragment(ImageArticleFragment articleFragment) {
//        imageArticleFragments.add(articleFragment);
//    }

    public void removeArticleFragment(AbstractArticleFragment articleFragment) {
        //todo ver como hace el remove
    }

    public TextArticleFragment getTextArticleFragment(int articleFragmentNumber) {
        for (int j = 0; j < textArticleFragments.size(); j++) {
            if (textArticleFragments.get(j).getOrderNumber() == articleFragmentNumber) {
                return textArticleFragments.get(j);
            }
        }
        return null;
    }

//    public ImageArticleFragment getImageArticleFragment(int articleFragmentNumber) {
//        for (int j = 0; j < imageArticleFragments.size(); j++) {
//            if (imageArticleFragments.get(j).getOrderNumber() == articleFragmentNumber) {
//                return imageArticleFragments.get(j);
//            }
//        }
//        return null;
//    }

//    @OneToMany(cascade = CascadeType.ALL)
//    public List<ImageArticleFragment> getImageArticleFragments() {
//        return imageArticleFragments;
//    }

    @OneToMany(cascade = CascadeType.ALL)
    public List<TextArticleFragment> getTextArticleFragments() {
        return textArticleFragments;
    }

    @Column(name = "READY")
    public boolean isReady() {
        return ready;
    }

    public void setReady(boolean ready) {
        this.ready = ready;
    }

    public void setTextArticleFragments(List<TextArticleFragment> textArticleFragments) {
        this.textArticleFragments = textArticleFragments;
    }

//    public void setImageArticleFragments(List<ImageArticleFragment> imageArticleFragments) {
//        this.imageArticleFragments = imageArticleFragments;
//    }
}
